package com.zyx.maildemo.controller;

import com.zyx.maildemo.exception.ServiceException;
import com.zyx.maildemo.utils.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @AUTHOR soft
 * @DATE 2018/12/20 22:55
 * @DESCRIBE 全局异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public RestResponse serviceHandler(ServiceException e) {
        log.error("Service: {}", e.getMessage());
        return RestResponse.fail(e.getMessage());
    }

    @ExceptionHandler
    @ResponseBody
    public RestResponse handler(Exception e) {
        log.error("{}", e.getMessage());
        return RestResponse.fail(e.getMessage());
    }
}
